package com.itheima.advantagesup.controller;

import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.dto.SysroleDTO;
import com.itheima.advantagesup.entity.PageBean;
import com.itheima.advantagesup.service.SysroleService;
import com.itheima.advantagesup.vo.SysroleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/sys_role")
public class SysroleController {
    @Autowired
    private SysroleService sysroleService;

    @GetMapping("/allData")
    public R findAllrole() {
        log.info("查询所有角色信息");
        List<SysroleDTO> list = sysroleService.findAll();
        return R.success(list);
    }
    /**
     * 角色管理分页查询
     *
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @GetMapping("/list")
    public R findAllrole(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit, String name) {
        log.info("分页查询:{}{}{}", page, limit, name);
        PageBean pageBean = sysroleService.findAllrole(page, limit, name);
        return R.success(pageBean);
    }

    /**
     * 角色添加
     *
     * @param sysroleDTO
     * @return
     */
    @PostMapping("/add")
    public R Addrole(@RequestBody SysroleDTO sysroleDTO) {
        log.info("添加角色信息:{}", sysroleDTO);
        sysroleService.addRole(sysroleDTO);
        return R.success();
    }

    /**
     * 删除角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R delete(String id) {
        log.info("删除角色信息:{}", id);
        sysroleService.delete(id);
        return R.success();
    }

    /**
     * 获取角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R getById(String id) {
        log.info("获取角色信息:{}", id);
       SysroleVO sysroleVO = sysroleService.getById(id);
        return R.success(sysroleVO);
    }
    @PostMapping("/update")
    public R update(@RequestBody SysroleDTO sysroleDTO) {
        log.info("修改角色信息:{}", sysroleDTO);
        sysroleService.update(sysroleDTO);
        return R.success();
    }
}
